package com.bw.live_streaming.websocket;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Vector;


//医生聊天端
@Component
@CrossOrigin
@ServerEndpoint("/hospital/doctorchat")
public class Doctorchat {

    private Session session;

    public  static Vector<Doctorchat> clients=new Vector<Doctorchat>();

    @OnOpen
    public void onOpen(Session session){
        this.session=session;
        clients.add(this);
        System.out.println("加入聊天~！");
    }

    @OnClose
    public void onClose(){
        clients.remove(this);
        System.out.println("退出聊天~！");
    }

    //监听消息
    @OnMessage
    public void onMessage(String message,Session session){
        for (Doctorchat client:clients){
            try {
                client.session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
